﻿using AutoMapper;
using B.S.Orders.Domain;
using B.S.Orders.ErrorCord;
using B.S.Orders.Write.Api.Application.Command.DocumentsCommand;
using MediatR;
using SqlSugar;

namespace B.S.Orders.Write.Api.Application.CommandHander.DocumentsCommandHander
{
    public class UpdDocumentsCommandHander : IRequestHandler<UpdDocumentsCommand, APIResult<int>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public UpdDocumentsCommandHander(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(UpdDocumentsCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

            var model = mapper.Map<Documents>(request);

            model.personnel = "张隆铭";
            model.Time = DateTime.Now;

            model.Del = false;

            result.Data = await db.Updateable<Documents>(model).ExecuteCommandAsync();

            if (result.Data > 0)
            {
                result.Code = APIEnum.success;
                result.Msg = "修改成功";
            }
            else
            {
                result.Code = APIEnum.success;
                result.Msg = "修改失败";
            }

            return result;
        }
    }
}
